package com.myapplication.ui.myactivity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.myapplication.R;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.LogInListener;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by zitaojiang on 2016/10/19.
 */
public class LoginActivity extends AppCompatActivity {


    @Bind(R.id.tv_resig)
    TextView tvResig;
    private String name;
    private String password;
    private MyUser myUser;

    @Bind(R.id.et_name)
    EditText etName;
    @Bind(R.id.layout_name)
    TextInputLayout layoutName;
    @Bind(R.id.et_password)
    EditText etPassword;
    @Bind(R.id.layout_password)
    TextInputLayout layoutPassword;
    @Bind(R.id.btn_login)
    Button btnLogin;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        myUser = new MyUser();
        init();
        initLogin();
        initJump();
    }

    //跳转到注册页面
    private void initJump() {

        tvResig.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
                finish();
            }
        });

    }

    private void initLogin() {
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                name = etName.getText().toString();
                password = etPassword.getText().toString();
                if (name.isEmpty() || password.isEmpty()) {
                    Toast.makeText(LoginActivity.this, "账号或者密码不为空", Toast.LENGTH_LONG).show();
                    return;
                } else {

                    BmobUser.loginByAccount(name, password, new LogInListener<BmobUser>() {
                        @Override
                        public void done(BmobUser bmobUser, BmobException e) {
                            if (e==null){
                                Intent intent = new Intent(LoginActivity.this, MainInfoActivity.class);
                                 //intent.putExtra("objectid", a);
                                startActivity(intent);
                                finish();
                            }else {
                                Toast.makeText(LoginActivity.this, "密码错误", Toast.LENGTH_LONG).show();
                            }
                        }
                    });
                    
                    /*myUser.setName(name);
                    myUser.setPassword(password);
                    myUser.login(new SaveListener<BmobUser>() {
                        @Override
                        public void done(BmobUser bmobUser, BmobException e) {
                            if (e==null){
                                Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_LONG).show();
                            }else {
                                Toast.makeText(LoginActivity.this, "密码错误", Toast.LENGTH_LONG).show();
                            }
                        }
                    });*/
                   /* BmobQuery<MyUser> myUserQuery = new BmobQuery<MyUser>();
                    myUserQuery.addWhereEqualTo("name", name);
                    myUserQuery.addWhereEqualTo("password", password);*/

                  /*  myUserQuery.findObjects(new FindListener<MyUser>() {
                        @Override
                        public void done(List<MyUser> list, BmobException e) {
                            if (e == null) {
                                if (list != null && list.size() > 0) {
                                    String a = list.get(0).getObjectId();
                                    Intent intent = new Intent(LoginActivity.this, MainInfoActivity.class);
                                    intent.putExtra("objectid", a);
                                    startActivity(intent);
                                    Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_LONG).show();
                                } else {
                                    Toast.makeText(LoginActivity.this, "密码错误", Toast.LENGTH_LONG).show();
                                }

                            } else {

                                Toast.makeText(LoginActivity.this, "请检查网络！", Toast.LENGTH_LONG).show();

                            }
                        }
                    });*/

                }
            }
        });
    }

    private void init() {

        etName = layoutName.getEditText();
        etName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.length() < 5) {
                    layoutName.setErrorEnabled(true);
                    layoutName.setError("请输入正确邮箱");
                } else {
                    layoutName.setErrorEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        etPassword = layoutPassword.getEditText();
        etPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.length() < 6) {
                    layoutPassword.setErrorEnabled(true);
                    layoutPassword.setError("请输入不少于6位密码");
                } else {
                    layoutPassword.setErrorEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }


}
